from transformers import pipeline
import tkinter as tk
from threading import Thread


summarizer = pipeline("summarization", model="Junlaii/art_4acticle_abstract")

def on_send_button_click():
    message = input_box.get()
    chatlog.config(state=tk.NORMAL)  # 允许编辑
    chatlog.insert(tk.END, "发送: " + message + "\n")
    chatlog.config(state=tk.DISABLED)  # 禁止编辑
    input_box.delete(0, tk.END)

def get_reps(ARTICLE):
    ret = summarizer(ARTICLE, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
    return ret

# 定义发送消息的函数
def send_message(event=None):
    ARTICLE = input_box.get()
    if ARTICLE:
        chatlog.config(state=tk.NORMAL)
        chatlog.insert(tk.END, "我: " + ARTICLE + "\n\n")
        thread = Thread(target=async_send_message, args=(ARTICLE,))
        thread.start()
        input_box.delete(0, tk.END)


def async_send_message(ARTICLE):
    reps_result = get_reps(ARTICLE)
    root.after(0, update_chatlog, reps_result)


def update_chatlog(reps_result):
    chatlog.insert(tk.END, "机器人: " + reps_result + "\n\n")
    chatlog.config(state=tk.DISABLED)


root = tk.Tk()
root.title("文本摘要机器人")
root.minsize(400, 300)
root.resizable(width=False, height=False)


chatlog = tk.Text(root, width=100, height=40)
chatlog.config(state=tk.DISABLED)  # 禁止编辑
chatlog.pack()

# 创建输入框和发送按钮
input_frame = tk.Frame(root)
input_frame.pack(side=tk.BOTTOM, fill=tk.X)
input_box = tk.Entry(input_frame)
input_box.pack(side=tk.LEFT, fill=tk.X, expand=True)

send_button = tk.Button(input_frame, text="发送", command=send_message)
send_button.pack(side=tk.RIGHT)

# 绑定回车键
input_box.bind("<Return>", send_message)

# 进入主循环
root.mainloop()
